package cn.agiledata.bank.common.dao;

import java.util.List;

import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.hibernateBean.Bank;
import cn.agiledata.bank.hibernateBean.BankInfo;
import cn.agiledata.bank.hibernateBean.VnvAccount;
import com.vnv.infrastructure.logic.IPBHibernate3BaseMgr;

public class BankDaoImpl extends IPBHibernate3BaseMgr implements BankDao {
    public Bank getBankByCode(String bankCode) {
        String hql = "from BankInfo b where b.bankCode=? and b.valid=? and b.supported=?";
        List resultList = getHibernateTemplate().find(
				hql,
				new Object[] { bankCode, Constant.INSIDE_STATUS_YES,
						new Integer(Constant.SUPPORT_NET_BANK) });
        BankInfo bank = resultList.size() == 0 ? null : (BankInfo) resultList
                .get(0);
        return bank == null ? null : new Bank(bank);
    }

    public List getBankList() {
        String hql = "from BankInfo b where b.valid = ?";
        return getHibernateTemplate().find(hql, Constant.VALID);
    }

    public VnvAccount getDefaultVnvAccountOfBank(String bankCode) {
        String hql = "from VnvAccount va where va.bankInfo.bankCode=?"
                + " and va.defaultFlag=? and va.status=?";
        List resultList = getHibernateTemplate().find(hql,
                new Object[] { bankCode, Constant.VALID, Constant.VALID });
        if (resultList.size() == 0)
            return null;
        else
            return (VnvAccount) resultList.get(0);
    }
    
    /*
     * (non-Javadoc)
     * @see cn.agiledata.bank.common.dao.BankDao#getBankByLinkCode(java.lang.String)
     */
	public Bank getBankByLinkCode(String linkCode) {
		// 根据直连银行编号查询有效的银行
		String hql = "from BankInfo b where b.linkCode=? and b.valid=? and b.supported=?";
        List resultList = getHibernateTemplate().find(
				hql,
				new Object[] { linkCode, Constant.INSIDE_STATUS_YES,
						new Integer(Constant.SUPPORT_NET_BANK) });
        if(resultList.size() != 1) {
        	// 相同直连编号的有效银行不唯一，返回空
        	return null;
        }

        return new Bank((BankInfo) resultList.get(0));
	}
}
